package com.quzi.kettle.activitise.added;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.quzi.kettle.GlobalConfig;
import com.quzi.kettle.R;
import com.quzi.kettle.bean.ModeItem;
import com.quzi.kettle.datapiece.status.DeviceStatus;
import com.quzi.kettle.utils.BoliModeUtil;
import com.quzi.kettle.utils.MSharePerference;

public class ModesEpAdapter extends BaseExpandableListAdapter {
	private List<TypeSpeMode> modelists;
	private Context context;

	public ModesEpAdapter(List<TypeSpeMode> modeslist, Context context) {
		this.modelists = modeslist;
		this.context = context;
	}

	private IEpListener listener;

	public void setIeplistener(IEpListener epListener) {
		this.listener = epListener;

	}

	@Override
	public int getGroupCount() {
		return modelists.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		MyGroupHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.mode_item, null);
			holder = new MyGroupHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (MyGroupHolder) convertView.getTag();
		}
		holder.tv_name.setText(modelists.get(groupPosition).getType());
		holder.iv_icon
				.setImageResource(modelists.get(groupPosition).getResid());
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		MyChildHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.view_mode2child, null);
			holder = new MyChildHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (MyChildHolder) convertView.getTag();
		}

		holder.list.setAdapter(new ArrayAdapter<String>(context,
				R.layout.view_modechild_item, getData(groupPosition)));
		holder.list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (groupPosition == 0) {

					DeviceStatus status = new DeviceStatus();
					status.whatmode = (byte) 1;
					// GlobalConfig.WHATMODE = (byte) 1;
					BoliModeUtil.saveBoliMode(context, 1);
					status.setFixedTemperatureTime = (byte) (modelists
							.get(groupPosition).getModeitemlist().get(position)
							.isNeedWarm() ? 120 : 0);
					status.get_rid_cl_work = (byte) (modelists
							.get(groupPosition).getModeitemlist().get(position)
							.isNeedBoiled() ? 1 : 0);
					listener.modeclick(
							status,
							modelists.get(groupPosition).getModeitemlist()
									.get(position).getModeTitle(),
							modelists.get(groupPosition).getModeitemlist()
									.get(position).getTargetTemperature());

				} else {
					DeviceStatus mStatus = new DeviceStatus();
					mStatus.setFixedTemperatureTime = 120;
					mStatus.get_rid_cl_work = 1;
					if (groupPosition <= 10) {
						mStatus.whatmode = (byte) 1;
						BoliModeUtil.saveBoliMode(context, 1);
					} else if (groupPosition == 11) {
						BoliModeUtil.saveBoliMode(context, 10);
						mStatus.whatmode = (byte) 10;
					} else if (groupPosition == 12) {
						BoliModeUtil.saveBoliMode(context, 11);
						mStatus.whatmode = (byte) 11;
					}
					listener.modeclick(
							mStatus,
							modelists.get(groupPosition).getSpemodelist()
									.get(position).getName(),
							modelists.get(groupPosition).getSpemodelist()
									.get(position).getTemp());

				}
			}
		});
		return convertView;
	}

	private List<String> getData(int position) {
		List<String> data = new ArrayList<String>();
		if (position != 0) {
			List<SpeMode> spemodelist = modelists.get(position)
					.getSpemodelist();
			for (SpeMode spemode : spemodelist) {
				data.add(spemode.getName());
			}
			Log.v("kettle", data.toString());
		} else {
			List<ModeItem> modeitemlist = modelists.get(position)
					.getModeitemlist();
			for (ModeItem modeItem : modeitemlist) {
				data.add(modeItem.getModeTitle());
			}
		}

		return data;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	class MyGroupHolder {
		private ImageView iv_icon;
		private TextView tv_name;
		private ImageView iv_right;

		public MyGroupHolder(View view) {
			iv_icon = (ImageView) view.findViewById(R.id.modeicon);
			tv_name = (TextView) view.findViewById(R.id.name);
		}

	}

	class MyChildHolder {
		private ListViewForScrollView list;;

		public MyChildHolder(View view) {
			list = (ListViewForScrollView) view
					.findViewById(R.id.lv_mode2_child);
		}
	}
}
